package com.ytt.core.api.controller.schoolClass;

import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.model.schoolClass.SchoolClassDto;
import com.ytt.core.model.schoolClass.SchoolClassEditDto;
import com.ytt.core.service.service.ProposalQueryParam;
import com.ytt.core.service.service.schoolClass.SchoolClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

@Api(tags = {"班级管理"})
@RestController
@RequestMapping("/api/schoolClass")
@Validated
public class SchoolClassController {
    private  final SchoolClassService schoolClassService;

    @Autowired
    public SchoolClassController(SchoolClassService schoolClassService) {
        this.schoolClassService = schoolClassService;
    }

    @ApiOperation(value = "根据ID查找班级")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('SCHOOLCLASS_RETRIEVE')")
    public SchoolClassDto findById(
            @ApiParam(value = "班级ID", required = true) @PathVariable String id) {
        return schoolClassService.findById(id);
    }

    @ApiOperation(value = "分页查找班级")
    @GetMapping(value = "")
    @PreAuthorize("hasAnyAuthority('SCHOOLCLASS_RETRIEVE')")
    public PageDataDTO<SchoolClassDto> findOnePage(
            @ApiParam(value = "页号，从0开始", defaultValue = "0") @RequestParam(value = "page", required = false) @Min(0) Integer page,
            @ApiParam(value = "每页纪录条数", defaultValue = "20") @RequestParam(value = "size", required = false) @Min(1) @Max(100) Integer size,
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(value = "班级名称") @RequestParam(value = "name", required = false) String name) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .build();
        return schoolClassService.findOnePage(page, size, sort, param);
    }

    @ApiOperation(value = "查找所有班级")
    @GetMapping(value = "/all")
    @PreAuthorize("hasAnyAuthority('SCHOOLCLASS_RETRIEVE')")
    public List<SchoolClassDto> findAll(
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", defaultValue = "id", required = false) String sort,
            @ApiParam(value = "班级名称") @RequestParam(value = "name", required = false) String name) {
        ProposalQueryParam param = ProposalQueryParam.builder()
                .name(name)
                .build();
        return schoolClassService.findAll(sort,param);
    }



    @ApiOperation(value = "创建班级")
    @PostMapping(value = "")
    @PreAuthorize("hasAnyAuthority('SCHOOLCLASS_CREATE')")
    public SchoolClassDto create(@ApiParam(value = "班级信息", required = true) @RequestBody @Validated SchoolClassEditDto schoolClassEditDto) {
        return schoolClassService.create(schoolClassEditDto);
    }

    @ApiOperation(value = "更新班级")
    @PutMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('SCHOOLCLASS_UPDATE')")
    public SchoolClassDto update(@ApiParam(value = "班级ID", required = true) @PathVariable String id,
                           @ApiParam(value = "班级信息", required = true) @RequestBody @Validated SchoolClassEditDto schoolClassEditDto) {
        return schoolClassService.update(id, schoolClassEditDto);
    }

    @ApiOperation(value = "删除班级")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('SCHOOLCLASS_DELETE')")
    public void delete(@ApiParam(value = "班级ID", required = true) @PathVariable String id) {
        schoolClassService.delete(id);
    }
}
